package modelTest.model.spawners;

import java.util.Random;

import modelTest.model.Coordinate;
import modelTest.model.GameStatus;
import modelTest.model.ShipMover;
import modelTest.model.Spawner;
import modelTest.model.mobs.Mob;
import modelTest.threadBasedModel.MobThread2D;

/**
 * Versione per testare una soluzione alternativa all'accesso concorrente della lista dei Mob.
 * @author Jan
 *
 */
public class Concurrent2DSpawner implements Spawner, Runnable {
	
	private GameStatus gameStatus;
	private ShipMover mover;
	private int meanTime;
	private int timeRange;
	private int coordMin;
	private int coordMax;
	private int averageShiftAmount;
	private Random randGen;
	
	
	public Concurrent2DSpawner(GameStatus gameStatus, int meanTime, int timeRange, int averageShiftAmount) {
		this.gameStatus = gameStatus;
		this.meanTime = meanTime;		//ms
		this.timeRange = timeRange;		//ms
		this.coordMin = Coordinate.MOB_2D_MIN_X;
		this.coordMax = Coordinate.MOB_2D_MAX_X;
		this.averageShiftAmount = averageShiftAmount;
		this.randGen = new Random();
	}
	
	
	@Override
	public void run() {
		Long ID = 0l;
		while (true) {
			if (gameStatus.isRunning()) {
				Coordinate coord = new Coordinate(randGen.nextInt(coordMax
						- coordMin)
						+ coordMin + 1, Coordinate.MOB_2D_SPAW_Y,
						Coordinate.MOB_2D_SPAW_Z);
				gameStatus.putMob(ID + "",
						new Mob(coord, (int) randGen.nextGaussian()
								+ averageShiftAmount));
				ID++;
			}
			
			try {
				Thread.sleep(randGen.nextInt(timeRange)
						+ (meanTime - timeRange / 2));
			} catch (InterruptedException e) {
				// TODO: Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
